package com.enjoy.mjbaseframe.util;

import android.app.Activity;
import android.content.Intent;

import com.joker.api.Permissions4M;
import com.joker.api.wrapper.ListenerWrapper;
import com.joker.api.wrapper.Wrapper;
import com.orhanobut.logger.Logger;

/**
 * 权限申请工具类
 * 框架：permissions4m
 * Created by Administrator on 2018/3/7.
 */

public class PermissionUtil {

    /**
     * 回调方式 申请权限
     * @param activity
     * @param code        权限申请码
     * @param permissions 权限集合
     */
    public static void requestPermission(final Activity activity, int code, String... permissions) {
        Wrapper wrapper = Permissions4M.get(activity);
        for(String permission : permissions){
            wrapper.requestPermissions(permission);
        }
        wrapper.requestCodes(code)
            .requestListener(new ListenerWrapper.PermissionRequestListener() {
                    @Override
                    public void permissionGranted(int code) {
                        Logger.d("权限申请成功");
                        questGranted(activity,code);
                    }

                    @Override
                    public void permissionDenied(int code) {
                        Logger.d("权限申请失败");
                        questDenied(activity,code);
                    }

                    @Override
                    public void permissionRationale(int code) {
                        Logger.d("二次授权时回调");
                        questRationale(activity,code);
                    }
                })
                .requestPageType(Permissions4M.PageType.MANAGER_PAGE)
                .requestPage(new Wrapper.PermissionPageListener() {
                    @Override
                    public void pageIntent(int code, final Intent intent) {
                        Logger.d("用户拒绝权限且不再提示情况下调用");
//                        DialogUIUtils.showB(activity, "", "用户您好，我们需要您开启读取通讯录权限申请：\n请点击前往设置页面\n", R.color.color_text_grlay1, R.color.font_new, "取消", "前往设置页面",
//                                false, true, true, new DialogUIListener() {
//                                    @Override
//                                    public void onPositive() {
//
//                                    }
//
//                                    @Override
//                                    public void onNegative() {
//                                        activity.startActivity(intent);
//                                    }
//
//                                }).show();
                    }
                })
                .request();
    }

    /**
     * 回调方式 申请权限
     * @param activity
     * @param code        权限集合
     * @param listener    权限回调类
     * @param permissions 权限申请码
     */
    public static void requestPermission(final Activity activity, int code, ListenerWrapper.PermissionRequestListener listener, String... permissions) {
        Wrapper wrapper = Permissions4M.get(activity);
        for(String permission : permissions){
            wrapper.requestPermissions(permission);
        }
        wrapper.requestCodes(code)
                .requestListener(listener)
                .requestPageType(Permissions4M.PageType.MANAGER_PAGE)
                .requestPage(new Wrapper.PermissionPageListener() {
                    @Override
                    public void pageIntent(int code, final Intent intent) {
                        Logger.d("用户拒绝权限且不再提示情况下调用");
//                        DialogUIUtils.showB(activity, "", "用户您好，我们需要您开启读取通讯录权限申请：\n请点击前往设置页面\n", R.color.color_text_grlay1, R.color.font_new, "取消", "前往设置页面",
//                                false, true, true, new DialogUIListener() {
//                                    @Override
//                                    public void onPositive() {
//
//                                    }
//
//                                    @Override
//                                    public void onNegative() {
//                                        activity.startActivity(intent);
//                                    }
//
//                                }).show();
                    }
                })
                .request();
    }

    /**
     * 权限申请成功 处理逻辑
     * @param code
     */
    public static void questGranted(Activity activity,int code) {

    }

    /**
     * 权限申请失败 处理逻辑
     * @param code
     */
    public static void questDenied(Activity activity,int code) {

    }

    /**
     * 二次授权时回调 处理逻辑
     * @param code
     */
    public static void questRationale(Activity activity,int code) {

    }
}
